package com.example.meituan.activity.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.widget.ViewPager2;

import com.example.meituan.R;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.activity.bean.CommentBean;
import com.example.meituan.activity.bean.ImgBean;
import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.activity.bean.ShopCarBean;
import com.example.meituan.activity.man.ManageManActivity;
import com.example.meituan.activity.man.StoreActivity;
import com.example.meituan.activity.man.addShopActivity;
import com.example.meituan.activity.man.frament.ManageHomeFragment;
import com.example.meituan.activity.man.frament.ManageMyFragment;
import com.example.meituan.activity.user.adapter.ImageSliderAdapter;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.CommentDao;
import com.example.meituan.dao.ImgDao;
import com.example.meituan.dao.ShopCarDao;
import com.example.meituan.dao.ShopDao;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;

import org.w3c.dom.Comment;

import java.util.ArrayList;
import java.util.List;

public class ProductDetialsActivity extends AppCompatActivity  {
    private ViewPager2 viewPager;
    private Handler handler = new Handler(Looper.getMainLooper());
    private Runnable runnable;
    private int currentPage = 0;
    ArrayList<String> imagePaths;
    private static final long DELAY_MS = 5000;
    AdminBean currentUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_product_detials);
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.product_detials_toolbar);
        androidx.appcompat.widget.SearchView searchView = toolbar.findViewById(R.id.detials_products_home_shop_listView_search);
        currentUser=AdminDao.getUserInfo(this);
        viewPager = findViewById(R.id.viewPager);
        imagePaths = new ArrayList<>();
        TextView priceTextView = findViewById(R.id.price);
        TextView soldTextView = findViewById(R.id.sold);
        TextView titleTextView = findViewById(R.id.title);
        TextView productDescriptionTextView = findViewById(R.id.product_des);

        TextView commentsTextView = findViewById(R.id.product_detials_comments);

        ImageView storeLogoImageView = findViewById(R.id.store_logo);
        TextView storeNameTextView = findViewById(R.id.store_name);
        TextView storeTypeTextView = findViewById(R.id.store_type);
        TextView toStoreButton = findViewById(R.id.to_store);
        TextView toComment=findViewById(R.id.to_comment);
        ShopBean shopBean = (ShopBean) getIntent().getSerializableExtra("shopBean");
        if(shopBean!=null){
            priceTextView.setText(shopBean.getPrice());
            soldTextView.setText("月销 "+ShopDao.getMonSale(shopBean.getId()));
            titleTextView.setText(shopBean.getName());
            productDescriptionTextView.setText(shopBean.getDes());
            BusinessBean businessBean= AdminDao.getBusiness(shopBean.getBusinessId());
            String path = businessBean.getsImg();
            Bitmap bitmap= BitmapFactory.decodeFile(path);
            storeLogoImageView.setImageBitmap(bitmap);
            storeNameTextView.setText(businessBean.getsName());
            storeTypeTextView.setText(businessBean.getsType());
            List<CommentBean> comments= CommentDao.getAllCommentsByShopId(shopBean.getId());
            commentsTextView.setText("评论("+comments.size()+")");

            // 设置轮播图适配器并启动自动滚动
            List<ImgBean> existingImages = ImgDao.getAllImagesByShopId(shopBean.getId());
            for (ImgBean img : existingImages) {
                imagePaths.add(img.getImg());
            }
            viewPager.setAdapter(new ImageSliderAdapter(this, imagePaths));
            viewPager.setCurrentItem(0);
            setupAutoScroll();
            toStoreButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BusinessBean businessBean= AdminDao.getBusiness(shopBean.getBusinessId());
                    Intent intent=new Intent(ProductDetialsActivity.this, StoreActivity.class);
                    intent.putExtra("businessBean",  businessBean.getsId());
                    startActivity(intent);
                }
            });

            TextView back=findViewById(R.id.product_detials_back);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();//返回功能
                    //finish();
                }
            });
            BottomNavigationView bottomNavigationView=findViewById(R.id.product_detials_bottom_menu);
            bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id=item.getItemId();
                    if(id==R.id.product_detials_bottom_menu_comment){
                        Intent intent=new Intent(ProductDetialsActivity.this, MessageActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("ifUser", "user");
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                    if (id == R.id.product_detials_bottom_menu_shop_car_add) {
                        // 加入购物车
                        ProductDetailsBottomSheetDialog dialog = new ProductDetailsBottomSheetDialog();
                        ProductDetailsBottomSheetDialog.ProductDetailsBottomSheetDialogListener listener =
                                new ProductDetailsBottomSheetDialog.ProductDetailsBottomSheetDialogListener() {
                                    @Override
                                    public void exit() {
                                        onBackPressed(); // 返回功能
                                    }
                                };

                        // 确保传递了正确的 FragmentManager 和 ShopBean
                        dialog.show(listener, shopBean, getSupportFragmentManager());
                    }
                    if(id==R.id.product_detials_bottom_menu_shop_car){
                        Intent intent=new Intent(ProductDetialsActivity.this, ShopCarActivity.class);
                        startActivity(intent);
                    }
                    if(id==R.id.product_detials_bottom_menu_buy) {
                        ShopBuy shopBuy=new ShopBuy();
                        ShopBuy.ShoupBuyL listener=new ShopBuy.ShoupBuyL() {
                            @Override
                            public void exit() {
                                onBackPressed();//返回功能
                                //finish();
                            }

                            @Override
                            public void toAddress() {
                                Intent intent=new Intent(ProductDetialsActivity.this, UserAddressActivity.class);
                                intent.putExtra("businessBean",  businessBean.getsId());
                                startActivity(intent);
                            }
                        };
                        shopBuy.show(listener,shopBean,getSupportFragmentManager());
                    }
                    return true;
                }
            });
        }

        toComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //转到评论页面
                CommentSheet dialog = new CommentSheet();

                dialog.show( shopBean, getSupportFragmentManager());
            }
        });
        }


    private void setupAutoScroll() {
        runnable = new Runnable() {
            @Override
            public void run() {
                if (viewPager != null) {
                    int count = viewPager.getAdapter().getItemCount();
                    currentPage = (currentPage + 1) % count;
                    viewPager.setCurrentItem(currentPage, true);
                }
                handler.postDelayed(this, DELAY_MS);
            }
        };

        handler.postDelayed(runnable, DELAY_MS);
    }

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacks(runnable);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (runnable != null) {
            handler.postDelayed(runnable, DELAY_MS);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }


}